<?php

class FormElement extends FormComponent
{

  /**
   * @var boolean
   */
  public $enabled = true;

  /**
   * @var string
   */
  public $field;

  /**
   * @var mixed
   */
  public $rules;

  /**
   *
   * @return string the element's database field
   */
  public function getField()
  {
    if (empty($this->field)) {
      $this->field = Inflector::underscore($this->getName());
    }

    return $this->field;
  }

  public function injectQueryParams(&$params)
  {
    $params['fields'][] = $this->getField();
  }

  protected function getDisplayValue(&$row)
  {
    return $this->getValue($row);
  }

  protected function getValue(&$row)
  {
    return $row[$this->getField()];
  }

  protected function setValue(&$row, $value)
  {
    $row[$this->getField()] = $value;
  }

  protected function getFieldValue(&$row, $name)
  {
    return $row[$name];
  }

  public function getClass(&$row, $action)
  {
    $class = array('form-element', $this->getTypeClass(), $this->getInstanceClass());

    if ($action != Form::ACTION_CREATE) {
      $class[] = $this->getRowClass($row);
    }

    $class = implode(' ', $class);

    return $class;
  }

  public function getTypeClass()
  {
    $class = get_class($this);
    return $this->classify($class);
  }

  public function getInstanceClass()
  {
    $class = get_class($this) . ' ' . $this->name;
    return $this->classify($class);
  }

  public function getRowClass(&$row)
  {
    $class = get_class($this) . ' ' . $this->name . ' ' . $this->getFieldValue($row, $this->form->pk);
    return $this->classify($class);
  }

  public function classify($class)
  {
    return str_replace('_', '-', Inflector::underscore($class));
  }

  public function setDataFromPost(&$row, $post, $action)
  {
    $this->setValue($row, $post->get($this->getField()));
  }

  public function render($row, $action)
  {
    $this->beforeRender($row, $action);

    switch ($action) {
      case Form::ACTION_INDEX :
        $output = $this->getDisplayValue($row);

        break;

      case Form::ACTION_VIEW :
        $value = $this->getDisplayValue($row);

        $output = '<span class="'.$this->getClass($row, $action).'"><label>'.$this->getTitle().':</label> <span class="form-element-body">'.$value.'</span></span>';

        break;

      default :
        $this->set('value', $this->getValue($row));
        $this->set('class', $this->getClass($row, $action));

        $this->set('action', $action);
        $this->set('name', $this->name);
        $this->set('title', $this->title);
        $this->set('typeClass', $this->getTypeClass());
        $this->set('instanceClass', $this->getInstanceClass());
        $this->set('enabled', $this->enabled);

        $output = $this->getView();
    }

    $output = $this->afterRender($output, $row, $action);

    return $output;
  }

  public function getTemplate()
  {
    $template = parent::getTemplate();

    if (empty($template)) {
      global $config;

      $template = $config['views_dir'] . '/form/' . Inflector::underscore(get_class($this)) . '_view.php';
    }

    return $template;
  }

}

?>